package com.bf.zk.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.bf.zk.constants.Constants;
import com.bf.zk.user.User;
import com.bf.tool.ConfirmInfo;

public class UserModify extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JDialog userModify;
	@SuppressWarnings("unused")
	private boolean flag = false;
	private JTextField userNewName;
	private JTextField userNewPwd;
	@SuppressWarnings("unused")
	private JFrame parent;
	private JLabel label0;

	public UserModify(JFrame parent, boolean modal) {
		this.parent = parent;
		JDialog.setDefaultLookAndFeelDecorated(true);
		userModify = new JDialog(parent, "用户修改操作", true);
		userModify.setSize(400, 300);
		userModify.setDefaultCloseOperation(2);
		// 居中显示
		userModify.setLayout(null);
		userModify.setLocationRelativeTo(parent);

		//

		if (true) {
			label0 = new JLabel("用 户 名：  " +  Constants.Modify1);
			label0.setBounds(50, 30, 300, 30);

			JLabel label1 = new JLabel("新用户名：");
			label1.setBounds(50, 70, 80, 30);
			userNewName = new JTextField("新用户名");
			userNewName.setBounds(130, 70, 200, 30);
			userModify.add(userNewName);
			userModify.add(label1);
		} 
		//
		JLabel label2 = new JLabel("新 密 码:");
		label2.setBounds(50, 130, 80, 30);
		userNewPwd = new JTextField("");
		userNewPwd.setBounds(130, 130, 200, 30);

		// 节点增加确定
		JButton userModifyCon = new JButton("确定");
		userModifyCon.setActionCommand("userModifyCon");
		userModifyCon.setBounds(50, 200, 130, 40);

		// 节点增加取消
		JButton userModifyCancel = new JButton("取消");
		userModifyCancel.setActionCommand("userModifyCancel");
		userModifyCancel.setBounds(220, 200, 130, 40);

		userModify.add(label0);
		userModify.add(label2);
		userModify.add(userNewPwd);
		userModify.add(userModifyCon);
		userModify.add(userModifyCancel);
		userModifyCon.addActionListener(this);
		userModifyCancel.addActionListener(this);
		userModify.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		// 用户修改确定
		if (e.getActionCommand().equals("userModifyCon")) {
			Connection conn = null;
			int rs=-1;
				String userName = userNewName.getText();
				String userPwd = userNewPwd.getText();
				if (Constants.Modify1.equals("admin")) {
					userName="admin";
				}
				if ((ConfirmInfo.ConfirmUser(userName))&& (ConfirmInfo.ConfirmPwd(userPwd))) {
					try {
						String fileName = "./sqlite/znzk.db";
						Class.forName("org.sqlite.JDBC");
						conn = DriverManager.getConnection("jdbc:sqlite:" + fileName);
						Statement stmt = conn.createStatement();
						rs = stmt.executeUpdate("UPDATE znzk_user set username='" + userName+ "',password='" + userPwd+ "' WHERE username='"+ Constants.Modify1 + "'");
						if (rs == 1) {
							Constants.addUser=new User();
							Constants.addUser.setUserName(userName);
							Constants.addUser.setUserPwd(userPwd);
						} else {
							JOptionPane.showMessageDialog(userModify, "用户已经存在!","警告", JOptionPane.WARNING_MESSAGE);
							//userNewName.setText("");
							//userNewPwd.setText("");
						}
					} catch (Exception e2) {
					}
					if (rs == 1) {
						Constants.userOrNot=true;
						userModify.setVisible(false);
						//parent.setVisible(true);
					}
					try {
						conn.close();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				} else if (!(ConfirmInfo.ConfirmUser(userName))&& (ConfirmInfo.ConfirmPwd(userPwd))) {
					JOptionPane.showMessageDialog(userModify, "用户名不符合规则(数字、字母、下划线组成)!", "警告",JOptionPane.WARNING_MESSAGE);
					//userNewName.setText("");
				}else if ((ConfirmInfo.ConfirmUser(userName))&& !(ConfirmInfo.ConfirmPwd(userPwd))){
					JOptionPane.showMessageDialog(userModify, "密码不符合规则(数字、字母、下划线组成，长度6至10位)!", "警告",JOptionPane.WARNING_MESSAGE);
					//userNewPwd.setText("");
				}else {
					JOptionPane.showMessageDialog(userModify, "用户名、密码不符合规则(数字、字母、下划线组成，密码6至10位)!", "警告",JOptionPane.WARNING_MESSAGE);
					//userNewName.setText("");
					//userNewPwd.setText("");
				}
		}

		// 用户修改取消
		if (e.getActionCommand().equals("userModifyCancel")) {
			Constants.Modify0 = "";
			Constants.Modify1 = "";
			Constants.Modify2 = "";
			Constants.userOrNot=false;
			Constants.userOrNotCan=true;
			userModify.setVisible(false);
		}
	}
}
